package dataserver.service.impl;

import dataserver.mapper.SceneVideoRepository;
import dataserver.pojo.SceneVideo;
import dataserver.service.SceneVideoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class SceneVideoServiceImpl implements SceneVideoService {
    private static Logger logger = LogManager.getLogger(SceneVideoServiceImpl.class.getName());

    @Autowired
    private SceneVideoRepository sceneVideoRepository;
    /**
     * 存储信息
     *
     * @param sourceData
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(SceneVideo sourceData) throws Exception {
        int t = 0;
        try {
            sourceData.setCreateTime(new Date());
            sceneVideoRepository.save(sourceData);
            t = 1;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        } finally {
            return t;
        }
    }
    /**
     * 删除信息根据id
     *
     * @param idS
     * @throws Exception
     */
    @Override
    public void deleteById(String idS) throws Exception {
        try {
            if (!StringUtils.isEmpty(idS)) {
                String[] ids = idS.split(",");
                for (String item : ids) {
                    int id = Integer.parseInt(item);
                    String updateBy = "1";
                    sceneVideoRepository.deleteByIdFalse(id, new Date(), updateBy);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param specification  查询条件
     * @param pageable 分页项
     * @return Page<SceneVideo>
     */
    @Override
    public Page<SceneVideo> findPageData(Specification<SceneVideo> specification, Pageable pageable) throws Exception {
        try {
            Page<SceneVideo> pageData;
            pageData = sceneVideoRepository.findAll(specification, pageable);
            return pageData;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
    }
    /**
     * 查询信息根据站场id
     *
     * @return List<SceneVideo>
     */
    @Override
    public List<SceneVideo> findData(Specification<SceneVideo> specification) throws Exception {
        try {
            return sceneVideoRepository.findAll(specification);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 验证非空
     *
     * @param sourceData
     * @throws Exception
     */
    protected void checkEmpty(SceneVideo sourceData) throws Exception {
//        if (StringUtils.isBlank(sourceData.getFenceName())) {
//            throw new Exception("管线名称是必填项，请验证。");
//        }
//        if (null == (sourceData.getFencedData())) {
//            throw new Exception("监测日期是必填项，请验证。");
//        }
    }
}
